package com.millstein.tsinglog.controller;

import com.millstein.tsinglog.annotation.ResponseResult;
import com.millstein.tsinglog.annotation.ShiroAccessLog;
import com.millstein.tsinglog.model.dto.role.PermissionAssignDTO;
import com.millstein.tsinglog.model.dto.role.RoleSaveDTO;
import com.millstein.tsinglog.model.pojo.Role;
import com.millstein.tsinglog.model.vo.result.PageResult;
import com.millstein.tsinglog.service.RoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/role")
@ResponseResult
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取所有的角色信息，注意不查超管
     * @return
     */
    @GetMapping("/getAllRoles")
    @RequiresPermissions(value = {"user:assignRole", "user:insert", "user:update"},
            logical = Logical.OR)
    public List<Role> getAllRoles() {
        return roleService.getAllRoles();
    }

    /**
     * 分页并条件查询角色信息
     * @param currentPage 当前页
     * @param pageSize 每页数据量
     * @param keyword 搜索关键字
     * @return
     */
    @GetMapping("/getRoleByPage")
    @RequiresPermissions("role:query")
    @ShiroAccessLog("获取角色信息")
    public PageResult<Role> getRoleByPage(Integer currentPage,
                                          Integer pageSize,
                                          String keyword) {
        return roleService.getRoleByPage(currentPage, pageSize, keyword);
    }

    /**
     * 修改角色的可用状态
     * @param id 角色id
     * @param usable 1可用，0禁用
     * @return
     */
    @GetMapping("/changeUsable")
    @RequiresPermissions("role:update")
    @ShiroAccessLog("修改角色的可用状态")
    public void changeUsable(Long id, Short usable) {
        roleService.changeUsable(id, usable);
    }

    /**
     * 根据ids批量删除角色信息
     * @param ids
     * @return
     */
    @GetMapping("/deleteByIds")
    @RequiresPermissions("role:delete")
    @ShiroAccessLog("删除角色信息")
    public void deleteByIds(@RequestParam("ids") List<Long> ids) {
        roleService.deleteByIds(ids);
    }

    /**
     * 新增角色
     * @param roleSaveDTO
     * @return
     */
    @PostMapping("/insertRole")
    @RequiresPermissions("role:insert")
    @ShiroAccessLog("新增角色信息")
    public void insertRole(@Valid @RequestBody RoleSaveDTO roleSaveDTO) {
        roleService.insertRole(roleSaveDTO);
    }

    /**
     * 更新角色
     * @param roleSaveDTO
     * @return
     */
    @PostMapping("/updateRole")
    @RequiresPermissions("role:update")
    @ShiroAccessLog("修改角色信息")
    public void updateRole(@Valid @RequestBody RoleSaveDTO roleSaveDTO) {
        roleService.updateRole(roleSaveDTO);
    }

    /**
     * 为角色分配权限
     * @param permissionAssignDTO
     * @return
     */
    @PostMapping("/assignPermission")
    @RequiresPermissions("role:assignPermission")
    @ShiroAccessLog("分配权限")
    public void assignPermission(@Valid @RequestBody PermissionAssignDTO permissionAssignDTO) {
        roleService.assignPermission(permissionAssignDTO);
    }

}
